var baseUrl = "http://172.16.182.57:8080/Tecnica_POC_Server";

Services = {};

Services.login = function(code, callback) {
	var xhr = Ti.Network.createHTTPClient();
	xhr.onload = function() {
		callback(JSON.parse(this.responseText).userEntity);
	};
	xhr.onerror = function() {
		Ti.API.error('error with call to server: ' + this.responseText);
	};
	xhr.open("POST", baseUrl + "/user/login");
	xhr.send({"code":code});
};

Services.getClientData = function(user, callback) {
	var xhr = Ti.Network.createHTTPClient();
	xhr.onload = function() {
		callback(JSON.parse(this.responseText).customerEntityList);
	};
	xhr.onerror = function() {
		Ti.API.error('error with call to server: ' + this.responseText);
	};
	xhr.open("POST", baseUrl + "/customer/user");
	xhr.send({"code":user.code});
};

Services.getCommandeData = function(client, callback) {
	var xhr = Ti.Network.createHTTPClient();
	xhr.onload = function() {
		callback(JSON.parse(this.responseText).orderEntityList);
	};
	xhr.onerror = function() {
		Ti.API.error('error with call to server: ' + this.responseText);
	};
	xhr.open("POST", baseUrl + "/order/customer");
	xhr.send({"code":client.code});
};

Services.getArticleData = function(commande, callback) {
	var xhr = Ti.Network.createHTTPClient();
	xhr.onload = function() {
		callback(JSON.parse(this.responseText).itemEntityList);
	};
	xhr.onerror = function() {
		Ti.API.error('error with call to server: ' + this.responseText);
	};
	xhr.open("POST", baseUrl + "/item/order");
	xhr.send({"num":commande.number});
};

Services.getTailleData = function(article, callback) {
	var xhr = Ti.Network.createHTTPClient();
	xhr.onload = function() {
		var result = JSON.parse(this.responseText).sizeEntityList;
		for (var i in result) {
			if (i >= 0) {
				result[i].division = article.division;
			}
		}
		callback(result);
	};
	xhr.onerror = function() {
		Ti.API.error('error with call to server: ' + this.responseText);
	};
	xhr.open("POST", baseUrl + "/size/item");
	xhr.send({
		"order":article.orderNumber,
		"line":article.lineNumber
	});
};

Services.updateTailleData = function(taille, callback) {
	var xhr = Ti.Network.createHTTPClient();
	xhr.onload = function() {
		callback();
	};
	xhr.onerror = function() {
		Ti.API.error('error with call to server: ' + this.responseText);
	};
	xhr.open("POST", baseUrl + "/size/update");
	xhr.send({
		"order":taille.orderNumber,
		"line":taille.itemLineNumber,
		"sizeId":taille.sizeId,
		"qty":taille.orderedQty,
		"discount":taille.discount,
		"netPrice":taille.netPrice
	});
};

Services.orderReportUrl = function(orderNum) {
	return encodeQueryData(baseUrl + '/order/report', {
		orderNum:orderNum
	});
};

Services.orderReportWithSignatureUrl = function(orderNum, path) {
	return encodeQueryData(baseUrl + '/order/report', {
		orderNum:orderNum,
		path:path
	});
};
